<?php
require_once('init.php');
if (!Auth::isAdmin()) {
	header('Location: login_form.php');
	exit();
}

		// odbieramy dane z formularza 
			$id = $_POST['id'];
			$time_lap = $_POST['time_lap']; 
			$msg = "";
			if($id and $time_lap) 
			{      
				// uaktualniamy tabel� 
				$ins = @mysql_query("UPDATE times SET id='$id', time_lap='$time_lap' WHERE id=$id"); 
				 mysql_query($ins);
					if($ins) 
					$msg = "<br/>Yeah! You Did It! ,-)<br/>"; 
				else $msg = "Error - something goes wrong ' . mysql_error . '"; 
			} 
			echo "<p></p>UPDATE times SET id='$id', time_lap='$time_lap' WHERE id=$id";
		?>

<!DOCTYPE html>
<html>
<head>
<title>Silesia Racing Time Battle</title>
<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]--> 
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="js/jquery-1.3.1.min.js"></script>  
<script type="text/javascript" src="js/jquery.betterTooltip.js"></script>  
<script type="text/javascript" src="js/jquery-latest.js"></script> 
<script type="text/javascript" src="js/jquery.tablesorter.js"></script> 
</head>
<body>

    <div id="wrapper">
        <div id="headerwrap">
        <div id="header">
           <p align="right"> Silesia Racing Time Battle EVOLution</p>
        </div>
        </div>
		<div id="topwrap">
        <div id="top_home">		
           <h2>Time Battle - Edit Drivers</h2>
        </div>
        </div>
        <div id="leftcolumnwrap">
        <div id="leftcolumn">
            <?php include_once ('menu_left.php');?>
        </div>
        </div>
        <div id="contentwrap">
        <div id="content">
		<div>   
		<p></p>
		<?php 
	$query = <<< EOF
	SELECT d.id AS id, d.description AS name, GROUP_CONCAT(t.time_lap) as times, d.model, d.engine, d.horsepower, d.torque, d.weight
	FROM times t
	INNER JOIN driver d ON t.driver_id = d.id
	GROUP BY t.driver_id
EOF;
	$data = array();
	$maxTimesLength = 0;
	
	$result = mysql_query($query) or die('Invalid query: ' . mysql_error());
	
	while ($row = mysql_fetch_assoc($result))
	{
		$row['times'] = explode(',', $row['times']);
		$row['total'] = $time = 0;		
		
		foreach($row['times'] as $time)
		{		
			$time = explode(':', $time);
			$row['total'] += $time[0] * 3600 + $time[1] * 60 + $time[2];
		}

		$m = floor($row['total'] / 3600);
		$s = floor(($row['total'] - $m * 3600) / 60);
		$ms = $row['total'] - $m * 3600 - $s * 60;
		$row['total'] = sprintf('%02s:%02s:%02s', $m, $s, $ms);
			
		if (count($row['times']) > $maxTimesLength)
			$maxTimesLength = count($row['times']);
			
		$data[] = $row;
	}
	
	foreach ($data as $rec)
		$rec['times'] = array_pad($rec['times'], $maxTimesLength, '');
?>
	<table id="table-2" class="tablesorter">
		<thead>
			<th>Name</th>
			<?= implode('', array_fill(0, $maxTimesLength, '<th>Time</th>')) ?>
			<th>Total</th>
		</thead>
		<tbody>
<?php	foreach($data as $record) : ?>	
			<tr>
				<td>
					<span class="tooltip" href="#"><?= $record['name'] ?>
						<span class="custom info"><img src="images/tooltip/Info.png" alt="Information" height="48" width="48" />
							<em>Information</em>
								Model: <?= $record['model'] ?><br/> 
								Engine: <?= $record['engine']  ?><br/> 
								Horsepower: <?= $record['horsepower']  ?>Hp <br/>
								Torque: <?= $record['torque']  ?> Nm <br/> 
								Weight: <?= $record['weight']  ?> Kg
						</span>
					</span>
				</td>
				<?
		$id = (int)$_POST['id'];
			//echo "$id";
		?>
				<?
				$query2 = "SELECT id, time_lap FROM times WHERE driver_id = {$record['id']}";
				$result2 = mysql_query($query2); 
				$query3 = "SELECT count(id) as cnt FROM times GROUP BY driver_id ORDER BY cnt DESC limit 1";
				$result3 = mysql_query($query3);
				$cnt = mysql_fetch_array($result3);
				$max = $cnt['cnt'];
				$i = 0;
				while($row = mysql_fetch_array($result2))
				{
				?><form action="drivertime_edit.php" method="post"><?
					echo "<td><input type='text' name='time[{$row['id']}]' value='{$row['time_lap']}' /><input type=submit value=U></td></form>";
					$i++;
				}
				for ($j=0; $j<$max-$i;$j++)
				{
					echo "<td>&nbsp;</td>";
				}
				
				?>
				<? /* =implode('</td><td>', array_pad($record['times'], $maxTimesLength, ''))*/ ?>
				<td><?= $record['total'] ?></td>	
			</tr>
<?php	endforeach; ?>
		</tbody>
	</table>
		</div>
        </div>
        </div>
        <div id="rightcolumnwrap">
        <div id="rightcolumn">
			<?php include_once ('right_column.php');?>
			<p></p>
        </div>
        </div>
        <div id="footerwrap">
        <div id="footer">
            <p>Silesia Racing Time Battle</p>
        </div>
        </div>
    </div>
</body>
</html>
